<template>
  <div>
    <div class="myTest" v-html="lastMsg" ref="scrollRef" />
    <el-row style="margin-top: 10px; margin-bottom: 10px">
      <el-col :span="1">
        <IconifyIconOffline :icon="folder" class="myICON" />
      </el-col>
      <el-col :span="3">
        <el-select v-model="AIModel" class="mySelect">
          <el-option value="1" label="上网达人" />
          <el-option value="2" label="知识达人" />
          <el-option v-if="userInfo.aiFalg === 1" value="3" label="数据专家" />
          <el-option value="4" label="绘画达人" />
          <el-option value="5" label="文案专家" />
        </el-select>
      </el-col>
      <el-col :span="18"
        ><el-input
          type="text"
          @keyup.enter="send"
          class="msgText"
          v-model="msg"
          placeholder="请输入聊天内容！"
          :rows="1"
      /></el-col>
      <el-col :span="2"
        ><el-button @click="send" type="primary" style="width: 100%"
          >发送消息</el-button
        ></el-col
      >
    </el-row>
  </div>
</template>
<script lang="ts" setup>
import folder from "@iconify-icons/ep/folder-add";
import getURL from "@/utils/http/address";
import { useNav } from "@/layout/hooks/useNav";
import { onMounted, ref, watch } from "vue";
import { getAIConfig } from "@/api/AICode";
import { useUserStoreHook } from "@/store/modules/user";

const userInfo = useUserStoreHook();
import {
  AIhttp,
  msg_content,
  showLoading,
  MessageList,
  AIModel,
  aiConfig,
  newDD,
  myscrollRef
} from "./AIUtils";

import { Marked } from "marked";
import { markedHighlight } from "marked-highlight";
import hljs from "highlight.js";
import "highlight.js/styles/atom-one-dark.css";

const { userNiChen, userHeader } = useNav();
const scrollRef = ref(myscrollRef);
const marked = new Marked(
  markedHighlight({
    langPrefix: "hljs language-",
    highlight(code, lang) {
      const language = hljs.getLanguage(lang) ? lang : "shell";
      return hljs.highlight(code, { language }).value;
    }
  })
);

const msg = ref(); //发送消息端
const lastMsg = ref(""); //与显示相关的消息

//let newDD;
let charIndex = 0;
onMounted(async () => {
  const Result = JSON.parse((await getAIConfig()).result)[0];
  aiConfig.value = Result;

  newDD.value = document.createElement("div");
  newDD.value.className = "RonHead";
  newDD.value.innerHTML =
    "<img src='" +
    getURL +
    aiConfig.value.aiHead +
    "'/><span>" +
    aiConfig.value.aiName +
    "</span>";
  myscrollRef.value.appendChild(newDD.value);

  msg_content.value = "";
  newDD.value = document.createElement("div");
  myscrollRef.value.appendChild(newDD.value);

  stopImg();
  writeChar();
  charIndex = 0;
});

function writeChar() {
  if (charIndex < aiConfig.value.aiSysMessage.length) {
    newDD.value.innerHTML += aiConfig.value.aiSysMessage.charAt(charIndex);
    charIndex++;
    setTimeout(writeChar, 30); // 每0.15秒添加一个字符
  }
}

//用户发送消息
const send = async () => {
  showLoading.value = true;
  MessageList.push({
    role: "user",
    content: msg.value
    //content: msg.value
  }); //添加用户会话

  newDD.value = document.createElement("div");
  newDD.value.className = "userHead";
  newDD.value.innerHTML =
    "<img src='" +
    userHeader.value +
    "'/><span>" +
    userNiChen.value +
    "</span>";
  scrollRef.value.appendChild(newDD.value);

  newDD.value = document.createElement("div");
  newDD.value.textContent = msg.value;
  msg.value = "";
  scrollRef.value.appendChild(newDD.value);
  showLoading.value = true;

  AIhttp(MessageList);
  msg_content.value = "";

  newDD.value = document.createElement("div");
  newDD.value.className = "RonHead";
  newDD.value.innerHTML =
    "<img src='" +
    getURL +
    aiConfig.value.aiHead +
    "'/><span>" +
    aiConfig.value.aiName +
    "</span>";
  scrollRef.value.appendChild(newDD.value);

  newDD.value = document.createElement("div");
  scrollRef.value.appendChild(newDD.value);
};

watch(msg_content, () => {
  newDD.value.innerHTML = marked.parse(msg_content.value);
  myscrollRef.value.scrollTop = myscrollRef.value.scrollHeight;
});

watch(showLoading, () => {
  if (showLoading.value == false) {
    stopImg();
  }
});

const stopImg = () => {
  const img = document.querySelectorAll(".RonHead img");
  img.forEach(el => {
    el.style.animation = "none";
    el.style.transform = "rotate(0deg)";
  });
};
</script>

<style>
.myTest {
  height: calc(100vh - 180px);
  padding: 10px;
  overflow-y: scroll;
  font-size: 16px;
  line-height: 35px;
  background: #fafafa;
}

::-webkit-scrollbar {
  display: none;
}

.userHead {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  margin-top: 15px;
  font-weight: bold;
}

.userHead img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
}

.userHead span {
  padding-left: 15px;
  font-size: 18px;
}

.RonHead {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  margin-top: 15px;
  font-weight: bold;
  color: #409eff;
}

.RonHead img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
  animation: rotate 2s linear infinite;
}

.RonHead span {
  padding-left: 15px;
  font-size: 18px;
}

.msgText {
  padding-right: 15px;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

.myICON {
  width: 30px;
  height: 30px;
  color: #797777;
}

.mySelect {
  width: 95%;
}
</style>
